﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using BUS;
using Public;


namespace QuanLyKhoHangVatTu
{
    public partial class ChiTietNhomKH_NCC : DevExpress.XtraEditors.XtraForm, IChildForm
    {
        NhomKHNCC_BUS nhom_bus = new NhomKHNCC_BUS();
        public ChiTietNhomKH_NCC()
        {
            InitializeComponent();
        }
        
        private void ChiTietNhomKH_NCC_Load(object sender, EventArgs e)
        {
            Nap();
            
        }

        private void gridView1_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            //NhomKHNCC_PB.MaNhom = gridView1.GetRowCellValue(e.FocusedRowHandle, "MANHOM").ToString();
            //NhomKHNCC_PB.TenNhom = gridView1.GetRowCellValue(e.FocusedRowHandle, "TENNHOM").ToString();
            //NhomKHNCC_PB.DienGiai = gridView1.GetRowCellValue(e.FocusedRowHandle, "DIENGIAI").ToString();
            //NhomKHNCC_PB.Loai = gridView1.GetRowCellValue(e.FocusedRowHandle, "LOAI").ToString();
        }
        public void Nap()
        {
            gridNhom.DataSource = nhom_bus.LayDSKHNCC();
        }
        public void Them()
        {
            NhomKhachHangNCC nhom = new NhomKhachHangNCC();
            nhom.Tag = this;
            nhom.ShowDialog();
            Nap();
        }

        public void Sua()
        {
            // lay gia tri dang chọn tren grd moi dung

            DataRow r = gridView1.GetFocusedDataRow();
            if (r == null) return;

            NhomKhachHangNCC nhom = new NhomKhachHangNCC(r["MANHOM"].ToString(), r["TENNHOM"].ToString(), r["DIENGIAI"].ToString(), r["LOAI"].ToString());

            nhom.Tag = this;
            nhom.ShowDialog();
            Nap();
        }
        public void Xoa()
        {
            DataRow r = gridView1.GetFocusedDataRow();
            if (r == null) return;
            if (XtraMessageBox.Show(string.Format("Bạn có chắc muốn xóa nhóm <{0}>?"
                , r["MANHOM"]), "Xác nhận"
                 , MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.No)
                return;
            try
            {
                nhom_bus.XoaNhomKHNCC(r["MANHOM"].ToString());
            }
            catch (Exception ex)
            {
                if (ex.Message.Contains("conflicted with the REFERENCE constraint"))
                {
                    XtraMessageBox.Show("Không thể xóa vì có dữ liệu quan hệ", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
            Nap();
        }
    }
}